package com.central.test

import java.util

import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
import com.central.controller.{ActivityTime, InitClueConditionEntity, InitComplexClueConditionEntity}
import com.central.service.{NewClueService}
import com.central.utils.LoadESData
import org.apache.spark.sql.SparkSession

object TestClue {
  def main(args: Array[String]): Unit = {
    try {
      val jspara = new JSONObject()
      jspara.put("jobId", "1000")
      jspara.put("relationship", "and")
      jspara.put("hasliferow", "0")
      jspara.put("fuzzyway", "0")

      var acttime: JSONArray = new JSONArray()
      val time: JSONObject = new JSONObject()
        time.put("days","2020-03-01~2020-03-11")
        time.put("time","10:43:06~11:43:06")
      acttime.add(time)

      jspara.put("activitytimes", acttime)
      var paras: JSONArray = new JSONArray()
      for (i <- 0 until (5)) {
        var a : JSONArray = new JSONArray()
        val jp: JSONObject = new JSONObject()
        jp.put("endtime", "1584516309000")
        jp.put("begintime", "1582874709000")
        jp.put("threshold", "2")
        jp.put("didAppear", "1")


        if (i == 0) {
          jp.put("siteNumber", "1")
        } else if(i==1) {
          jp.put("siteNumber", "5")
        }
        else if(i==2) {
          jp.put("siteNumber", "7")
        }
        else if(i==3) {
          jp.put("siteNumber", "3")
        }
        else {
          jp.put("siteNumber", "2")
        }
        a.add(jp)
        paras.add(a)
      }
      jspara.put("conditions", paras)
      println(jspara.toJSONString)
      if (args.length == 0) {
        val jsb: JSONObject = JSON.parseObject(jspara.toJSONString)
        val jobId = jsb.getString("jobId")
        val relationship = jsb.getString("relationship")
        val hasliferow = jsb.getString("hasliferow")
        val fuzzyway = jsb.getString("fuzzyway")

        val activityTimes = jsb.getJSONArray("activitytimes").toArray().map(jobj => {
          val timeMap = jobj.asInstanceOf[util.Map[String, Object]]
          val day = String.valueOf(timeMap.get("days"))
          val time = String.valueOf(timeMap.get("time"))
          ActivityTime(day, time)
        }).toList

        val jsa: JSONArray = jsb.getJSONArray("conditions")
        var seq: Seq[String] = Seq[String]()
        for (i <- 0 until (jsa.size())) {
          seq = seq :+ (jsa.get(i) + "")
        }
        var finalConditions: List[List[InitClueConditionEntity]] = List[List[InitClueConditionEntity]]()
        seq.map(s => {
          var aaa = List[InitClueConditionEntity]()
          val jsas: JSONArray = JSON.parseArray(s)
          for (i <- 0 until (jsas.size())) {
            val conditionMap = jsas.get(i).asInstanceOf[util.Map[String, Object]]
            val siteNumber = String.valueOf(conditionMap.get("siteNumber"))
            val beginTime = String.valueOf(conditionMap.get("begintime"))
            val endTime = String.valueOf(conditionMap.get("endtime"))
            val didAppear = String.valueOf(conditionMap.get("didAppear"))
            val threshold = String.valueOf(conditionMap.get("threshold"))
            val ent = InitClueConditionEntity(siteNumber, beginTime.toLong, endTime.toLong, didAppear.toInt, threshold.toInt)
            aaa = aaa :+ ent
          }
          finalConditions = finalConditions :+ aaa
        })
        val properties = LoadESData.loadProperties();
        val para1 = SparkSession
          .builder()
          .appName(jobId)
          //          .master("spark://10.68.2.240:7077")
          .master("local")
          .config("es.nodes", properties.getProperty("elasticsearch.host", "192.168.5.180"))
          .config("es.port", properties.getProperty("elasticsearch.port", "9200"))
          .config("es.mapping.date.rich", "false")
          .getOrCreate()
        para1.sparkContext.setLogLevel("ERROR")
        new NewClueService().clueAll(para1, InitComplexClueConditionEntity(jobId, relationship, finalConditions,activityTimes,hasliferow,fuzzyway))
      } else {
        //        log.error("参数个数不对，任务参数应放置在一个Json字符串里")
      }

    } catch {
      case e: Exception => {
        //        log.error(e.getMessage)
      }
    }

  }
}
